﻿using EF_coreDemo;
using Microsoft.EntityFrameworkCore;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace 一对多关系_单向导航
{
    public class MyDbContext : DbContext 
    {
        public DbSet<User> Users { get; set; }
        public DbSet<Leave> Leaves { get; set; }

        protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
        {
            base.OnConfiguring(optionsBuilder);
            optionsBuilder.UseSqlServer("Server=.;Database=demo4;Trusted_Connection=True;ConnectRetryCount=0;Encrypt=false;");
        }

        protected override void OnModelCreating(ModelBuilder modelBuilder)
        {
            base.OnModelCreating(modelBuilder);
         
            //配置User的映射关系
            modelBuilder.Entity<User>(b =>
            {
                  b.ToTable("T_Users");
                  b.Property(u => u.Name).IsRequired().HasMaxLength(100).IsUnicode();
            });
       
            //配置Leave的映射关系及一对多单向导航
            modelBuilder.Entity<Leave>(b =>
            {
                       b.ToTable("T_Leaves");
                       b.HasOne<User>(l => l.Requester).WithMany(); //设置申请人的单向导航
                       b.HasOne<User>(l => l.Approver).WithMany(); //设置审批人的单向导航
            });
         }
    }
}
